package com.bc.commonentity.log;

import com.bc.commoncore.util.JsonUtil;
import com.bc.commoncore.util.StringUtils;
import com.bc.commoncore.util.SystemUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ErrorEmail {
	private static Map<String, Date> mailMap=new HashMap<String, Date>(); 
	private static long span=(long)5*60*1000;//5分钟内不重复发送邮件
	/**
	 * 请求日志异常时发送邮件(5分钟内不重复发送邮件)
	 * @param single_key
	 * @param title
	 * @param body
	 * @return
	 */
	public static synchronized boolean sendAlertEmail(String single_key, String title, String body) {
//		boolean needSend=true;
//		if (mailMap.containsKey(single_key)) {
//			long dif=(new Date()).getTime()-mailMap.get(single_key).getTime();
//			if (dif>=span) {
//				mailMap.remove(single_key);
//				mailMap.put(single_key, new Date());
//			}else {
//				needSend=false;
//			}
//		}else {
//			mailMap.put(single_key, new Date());
//		}
//		if (!needSend) {
//			return false;
//		}
		return SystemUtils.sendAlertEmail(title, body);
	}
	/**
	 * 业务异常时发送邮件(5分钟内不重复发送邮件)
	 * @param title
	 * @param e
	 */
	public static void sendErrorMail(String title,Exception e){
		if (title==null||title.trim().isEmpty()) {
			return;
		}
		List<String> ipinfoList = SystemUtils.getLocalIpInfo();
		String appServerIP = JsonUtil.obj2string(ipinfoList);
		String body="appServerIP:"+appServerIP+"\n";
		if (e!=null) {
			body+=(e.getMessage()+StringUtils.getStackTrace(e));
		}
		sendAlertEmail(title,title, body);
	}
}
